package com.zookeeper.te2;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * 作者: LCG
 * 日期: 2019/9/17 11:53
 * 描述:
 */
public class MyZkLock {

    public static void main(String[] args) throws Exception {

        RetryPolicy retryPolicy=new ExponentialBackoffRetry(1000,3);

        CuratorFramework curatorFramework=CuratorFrameworkFactory.newClient("192.168.137.1:2181",retryPolicy);

        curatorFramework.start();

        InterProcessMutex interProcessMutex=new InterProcessMutex(curatorFramework,"/YYY/lock_");

        for(int i=0;i>=10;i++){
            interProcessMutex.acquire();//获取锁

            System.out.println("===========执行业务========");

            interProcessMutex.release();
        }

        curatorFramework.close();
    }


}
